﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EMS
{
    public partial class UpdateOrPayment : Form
    {
        string connectionString = ConfigurationManager.ConnectionStrings["OrlcConnect"].ConnectionString;
        string registerPlaceGlobal = "";
        int recordIdGlobal = 0;
        float firstPointGl, lastPointGl;

        public UpdateOrPayment(int recordId, int customerId, string customerName, int month, int year, float firstPoint, float lastPoint, string registerPlace, float price)
        {
            InitializeComponent();
            txtCustomerId.Text = customerId.ToString();
            txtCustomerName.Text = customerName;
            txtMonth.Text = month.ToString();
            txtYear.Text = year.ToString();
            txtFirstPoint.Text = firstPoint.ToString();
            txtLastpoint.Text = lastPoint.ToString();
            txtPrice.Text = price.ToString();
            txtRegisterPlace.Text = registerPlace;
            registerPlaceGlobal = registerPlace;
            firstPointGl = firstPoint;
            lastPointGl = lastPoint;
            recordIdGlobal = recordId;

            if (registerPlaceGlobal.Equals("Hai Chau"))
                {
                    var connection = new OracleConnection(connectionString);
                    try
                    {
                        connection.Open();
                        var cmd = new OracleCommand()
                        {
                            Connection = connection,
                            CommandText = String.Format(@"SELECT e.recordid, c.customerid, c.registerplace, p.name, e.firstpoint, e.lastpoint, c.CMND, t.price, pa.datepaid
                                    FROM customer c JOIN person p ON c.personid = p.personid
                                    JOIN type t ON t.typeid = c.typeid
                                    JOIN electricrecord e ON e.customerid = c.customerid
                                    JOIN payment pa ON pa.recordid = e.recordid
                                     WHERE e.month = {0} and e.year = {1} and pa.recordid = {2}", month, year, recordId),
                            CommandType = CommandType.Text
                        };

                        OracleDataReader reader = cmd.ExecuteReader();
                        if (reader.Read() != false)
                        {
                            btnPaid.Enabled = false;
                            btnUpdate.Enabled = false;
                        }
                        else
                        {
                            btnPaid.Enabled = true;
                            btnUpdate.Enabled = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            else if (registerPlaceGlobal.Equals("Thanh Khe"))
            {
                var connection = new OracleConnection(connectionString);
                try
                {
                    connection.Open();
                    var cmd = new OracleCommand()
                    {
                        Connection = connection,
                        CommandText = String.Format(@"SELECT e.recordid, c.customerid, c.registerplace, p.name, e.firstpoint, e.lastpoint, c.CMND, t.price, pa.datepaid
                                    FROM customer c JOIN person p ON c.personid = p.personid
                                    JOIN type t ON t.typeid = c.typeid
                                    JOIN electricrecord@dblink_elec e ON e.customerid = c.customerid
                                    JOIN payment@dblink_elec pa ON pa.recordid = e.recordid
                                    WHERE e.month = {0} and e.year = {1} and pa.recordid = {2}", month, year, recordId),
                        CommandType = CommandType.Text
                    };

                    OracleDataReader reader = cmd.ExecuteReader();
                    if (reader.Read() != false)
                    {
                        btnPaid.Enabled = false;
                        btnUpdate.Enabled = false;
                    }
                    else
                    {
                        btnPaid.Enabled = true;
                        btnUpdate.Enabled = true;
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            float lastpoint = 0, firstpoint = 0 ;
            if (float.TryParse(txtLastpoint.Text, out lastpoint) && float.TryParse(txtFirstPoint.Text, out firstpoint))
            {
                if (lastpoint > firstpoint && lastpoint > 0 && firstpoint >= 0)
                {
                    var connection = new OracleConnection(connectionString);
                    try
                    {
                        connection.Open();

                        OracleCommand cmd = connection.CreateCommand();
                        OracleTransaction trans = connection.BeginTransaction();
                        cmd.Transaction = trans;
                        if (registerPlaceGlobal.Equals("Hai Chau"))
                        {
                            cmd.CommandText = String.Format(@"update electricrecord set lastpoint = '{0}', firstpoint = '{1}' where recordid = {2}", lastpoint, firstpoint, recordIdGlobal);
                            MessageBox.Show("Update Record Successfully (Hai Chau) !");
                        }
                        else
                        {
                            cmd.CommandText = String.Format(@"update electricrecord@dblink_elec set lastpoint = '{0}', firstpoint = '{1}' where recordid = {2}", lastpoint, firstpoint, recordIdGlobal);
                            MessageBox.Show("Update Record Successfully (Thanh Khe) !");
                        }
                        cmd.ExecuteNonQuery();
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Please Enter Correct Last Point, First Point !");
                }
            }
            else
            {
                MessageBox.Show("Please Enter Correct Number !");
            }
            //this.Show();
        }

        private void btnPaid_Click(object sender, EventArgs e)
        {
            this.Hide();
            (new Payment(recordIdGlobal, txtCustomerId.Text, txtCustomerName.Text, int.Parse(txtMonth.Text), int.Parse(txtYear.Text), firstPointGl, lastPointGl, registerPlaceGlobal, float.Parse(txtPrice.Text))).ShowDialog();
            this.Show();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }


    }
}
